/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package system.track;

/**
 *
 * @author David
 */
public class Track {

    private Block startPoint1;
    private Block startPoint2;

    public Track()
    {
        Block[] blocks = new Block[10];
        Switch[] switches = new Switch[7];
        Sensor[] sensors = new Sensor[15];

        for (int i = 1; i < sensors.length; i++)
        {
            sensors[i] = new Sensor(i);
        }

        for (int i = 1; i < switches.length; i++)
        {
            switches[i] = new Switch(i);
        }

        blocks[1] = new Block(1);
        blocks[1].addSensor(Direction.CLOCKWISE, sensors[1]);
        blocks[1].addSensor(Direction.CLOCKWISE, sensors[2]);
        blocks[1].addSensor(Direction.COUNTERCLOCKWISE, sensors[5]);
        blocks[1].addSensor(Direction.COUNTERCLOCKWISE, sensors[6]);
        blocks[1].setSwitch(Direction.CLOCKWISE, switches[1]);
        blocks[1].setSwitch(Direction.COUNTERCLOCKWISE, switches[3]);

        switches[1].setEntrance(blocks[1]);
        switches[3].setEntrance(blocks[1]);

        blocks[2] = new Block(2);
        blocks[2].addSensor(Direction.CLOCKWISE, sensors[9]);
        blocks[2].addSensor(Direction.CLOCKWISE, sensors[10]);
        blocks[2].addSensor(Direction.COUNTERCLOCKWISE, sensors[1]);
        blocks[2].setSwitch(Direction.CLOCKWISE, switches[5]);
        blocks[2].setSwitch(Direction.COUNTERCLOCKWISE, switches[1]);

        switches[1].setExit(SwitchAngle.ONE_EIGHTY_DEGREES, blocks[2]);
        switches[5].setEntrance(blocks[2]);

        blocks[3] = new Block(3);
        blocks[3].addSensor(Direction.CLOCKWISE, sensors[4]);
        blocks[3].addSensor(Direction.COUNTERCLOCKWISE, sensors[3]);
        blocks[3].setSwitch(Direction.CLOCKWISE, null);
        blocks[3].setSwitch(Direction.COUNTERCLOCKWISE, switches[2]);

        switches[2].setExit(SwitchAngle.NINETY_DEGREES, blocks[3]);

        Block4 block4 = new Block4();
        block4.addSensor(Direction.COUNTERCLOCKWISE, sensors[11]);
        block4.addSensor(Direction.CLOCKWISE, sensors[2]);
        block4.setSwitch(Direction.COUNTERCLOCKWISE, switches[6]);
        block4.setSwitch(Direction.CLOCKWISE, switches[1]);

        switches[1].setExit(SwitchAngle.NINETY_DEGREES, block4);
        switches[6].setExit(SwitchAngle.NINETY_DEGREES, block4);
        switches[2].setExit(SwitchAngle.ONE_EIGHTY_DEGREES, block4);
        switches[4].setExit(SwitchAngle.ONE_EIGHTY_DEGREES, block4);
        switches[2].setEntrance(block4);
        switches[4].setEntrance(block4);

        block4.setParkingBlock(Direction.COUNTERCLOCKWISE, blocks[3]);
        block4.setParkingSwitch(Direction.COUNTERCLOCKWISE, switches[2]);
        block4.setParkingSwitch(Direction.CLOCKWISE, switches[4]);

        blocks[4] = block4;

        blocks[5] = new Block(5);
        blocks[5].setSwitch(Direction.COUNTERCLOCKWISE, switches[4]);
        blocks[5].setSwitch(Direction.CLOCKWISE, null);
        blocks[5].addSensor(Direction.COUNTERCLOCKWISE, sensors[13]);
        blocks[5].addSensor(Direction.CLOCKWISE, sensors[8]);

        switches[4].setExit(SwitchAngle.ONE_EIGHTY_DEGREES, blocks[5]);

        block4.setParkingBlock(Direction.CLOCKWISE, blocks[5]);

        blocks[6] = new Block(6);
        blocks[6].setSwitch(Direction.COUNTERCLOCKWISE, switches[6]);
        blocks[6].setSwitch(Direction.CLOCKWISE, switches[3]);
        blocks[6].addSensor(Direction.CLOCKWISE, sensors[5]);
        blocks[6].addSensor(Direction.COUNTERCLOCKWISE, sensors[12]);

        switches[3].setExit(SwitchAngle.NINETY_DEGREES, blocks[6]);
        switches[6].setExit(SwitchAngle.ONE_EIGHTY_DEGREES, blocks[6]);

        blocks[7] = new Block(7);
        blocks[7].setSwitch(Direction.COUNTERCLOCKWISE, switches[5]);
        blocks[7].setSwitch(Direction.CLOCKWISE, switches[6]);
        blocks[7].addSensor(Direction.COUNTERCLOCKWISE, sensors[9]);
        blocks[7].addSensor(Direction.CLOCKWISE, sensors[11]);
        blocks[7].addSensor(Direction.CLOCKWISE, sensors[12]);

        switches[6].setEntrance(blocks[7]);
        switches[5].setExit(SwitchAngle.NINETY_DEGREES, blocks[7]);

        Block8 block8 = new Block8();
        Block9 block9 = new Block9();

        block8.setCCWBlock(block9);
        block9.setCWBlock(block8);

        blocks[8] = block8;
        blocks[9] = block9;

        blocks[8].setID(8);
        blocks[8].setSwitch(Direction.CLOCKWISE, switches[3]);
        blocks[8].addSensor(Direction.COUNTERCLOCKWISE, sensors[14]);
        blocks[8].addSensor(Direction.CLOCKWISE, sensors[6]);

        switches[3].setExit(SwitchAngle.ONE_EIGHTY_DEGREES, blocks[8]);

        blocks[9].setID(9);
        blocks[9].setSwitch(Direction.COUNTERCLOCKWISE, switches[5]);
        blocks[9].addSensor(Direction.CLOCKWISE, sensors[14]);
        blocks[9].addSensor(Direction.COUNTERCLOCKWISE, sensors[10]);

        switches[5].setExit(SwitchAngle.ONE_EIGHTY_DEGREES, blocks[9]);

        this.startPoint1 = blocks[1];
        this.startPoint2 = blocks[9];
    }

    public Block getStartPoint(int id)
    {
        Block returnVal = this.startPoint1;
        if (id == 2)
        {
            returnVal = this.startPoint2;
        }
        if(id > 2)
        {
            throw new IllegalArgumentException("Bad start point id.");
        }
        return returnVal;
    }
}
